package spring.rabbitstomp.message.sender;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import spring.rabbitstomp.message.constant.RabbitMqConstant;

import java.util.List;
import java.util.Set;

/**
 * 生产者-直连交换机模式
 * 此模式下路由键匹配，同一账号多处在线都可接收到消息，未登录账号无法接收推送
 * 且再登录账号不会有推送，路由消息丢失，适用于web端
 *
 * @author weikun
 */
@Slf4j
@Component
public class RabbitMqDirectSender{

    @Autowired
    public RabbitTemplate webRabbitTemplate;



	public void sendSingleMsg(Long receiveUserId) {
        try {
            this.webRabbitTemplate.convertAndSend(RabbitMqConstant.EXCHANGE_DIRECT,
                    RabbitMqConstant.QUEUE_SINGLE_MSG + receiveUserId, "我是单发消息~~~DirectExchange");
            log.info("消息发送成功...");
        }catch (Exception e){
            log.error("发送消息失败：",e);
        }
    }


	@Async
	public void sendGroupMsg(Long groupId) {
		try {
            webRabbitTemplate.convertAndSend(RabbitMqConstant.EXCHANGE_TPOIC,groupId.toString(),"我是topic消息");
			log.info("消息发送成功...");
		}catch (Exception e){
			log.error("发送消息失败：",e);
		}
	}


}
